﻿using System;
using System.Collections.Generic;
using System.Text;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.DAL;
using RFIDStockManagement.Common.Public.Enums;
namespace RFIDStockManagement.BLL
{
    public class InStockDataBuilder
    {
        private InStockBill _value;

        public InStockDataBuilder()
        {
            _value = new InStockBill();
            
        }

        public InStockDataBuilder(InStockBill value)
        {
            _value = value;
           
        }


        #region 属性
        /// <summary>
        /// 制单人
        /// </summary>
        public int BillerID
        {
            get
            {
                return _value.BillerID;
            }
            set
            {
                _value.BillerID = value;
            }
        }

        /// <summary>
        /// 仓库ID
        /// </summary>
        public long StockID
        {
            get
            {
                return _value.FStockID;
            }
            set
            {
                _value.FStockID = value;
            }
        }
        /// <summary>
        /// 入库人
        /// </summary>
        public string InStockPerson
        {
            get
            {
                return _value.FInStockPerson;
            }
            set
            {
                _value.FInStockPerson = value;
            }
        }

        /// <summary>
        /// 供应商ID
        /// </summary>
        public long SupplierId
        {
            get
            {
                return _value.FSupplierID;
            }
            set
            {
                _value.FSupplierID = value;
            }
        }

        /// <summary>
        /// 入库属性
        /// </summary>
        public EnumInStockProperty InStockProperty
        {
            get
            {
                return _value.FItemProperty;
            }
            set
            {
                _value.FItemProperty =value;
            }
        }

        #endregion

        #region 公有函数
        public void SetData(InStockBill value)
        {
            _value = value;

        }

       

        /// <summary>
        /// 添加入库单分录和明细信息
        /// </summary>
        /// <param name="itemid"></param>
        /// <param name="stockplaceid"></param>
        /// <param name="unitid"></param>
        /// <param name="rfid"></param>
        public void AddDetailInfo(long itemid, long stockplaceid, long unitid, string rfid)
        {
            InStockBillEntry curEntry = null;
            InStockDetail newDetail;

            foreach (InStockBillEntry x in _value.Entries)
            {
                if (x.FItemID == itemid && x.FUnitID == unitid)
                {
                    curEntry = x;
                    break;
                }
            }

            //添加当前分录信息进去
            if (curEntry == null)
            {
                curEntry = new InStockBillEntry();
                curEntry.FItemID = itemid;
                curEntry.FStockPlaceID = stockplaceid;
                curEntry.FUnitID = unitid;
                curEntry.FScanTime = DateTime.Now;
                _value.Entries.Add(curEntry);
            }

            //添加当前rfid明细进去
            newDetail = new InStockDetail();
            newDetail.FRFID = rfid;
            newDetail.FItemID = itemid;
            curEntry.RFIDDetails.Add(newDetail);

        }


        /// <summary>
        /// 添加一条入库单分录信息
        /// </summary>
        /// <param name="itemid"></param>
        /// <param name="stockplaceid"></param>
        /// <param name="unitid"></param>
        public void AddEntryInfo(long itemid, int stockplaceid,long unitid)
        {
            InStockBillEntry curEntry = null;
            foreach (InStockBillEntry x in _value.Entries)
            {
                if (x.FItemID == itemid)
                {
                    curEntry = x;
                    break;
                }
            }

            if (curEntry == null)
            {
                curEntry = new InStockBillEntry();
                curEntry.FItemID = itemid;
                curEntry.FStockPlaceID = stockplaceid;
                curEntry.FUnitID = unitid;
                curEntry.FScanTime = DateTime.Now;
                _value.Entries.Add(curEntry);
            }

        }

        /// <summary>
        /// 获取当前数据
        /// </summary>
        /// <returns></returns>
        public InStockBill GetData()
        {
            foreach (InStockBillEntry x in _value.Entries)
            {
                x.FQty = x.RFIDDetails.Count;
                
            }

            return _value;
        }

        #endregion
        
    }
}
